{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Artificial Bee Colony (ABC) is an optimization algorithm\n",
    "that is inspired by the behavior of honeybees. It is a type\n",
    "of metaheuristic algorithm, which means that it is designed\n",
    "to search for optimal solutions in complex, high-dimensional\n",
    "search spaces.\n",
    "\n",
    "The ABC algorithm works by simulating the behavior of a colony\n",
    "of bees as they forage for food. The colony is made up of\n",
    "three types of bees: employed bees, onlookers, and scouts.\n",
    "\n",
    "Employed bees are responsible for exploring the neighborhood\n",
    "around a food source. They do this by taking a candidate\n",
    "solution from their current position and modifying it slightly.\n",
    "The quality of the modified solution is then evaluated,\n",
    "and if it is better than the current solution,\n",
    "the employed bee returns to the hive and shares the new\n",
    "solution with the other bees.\n",
    "\n",
    "Onlooker bees observe the dances of the employed bees and\n",
    "choose a food source to visit based on the quality of the dance.\n",
    "The onlookers then visit the chosen food source and repeat\n",
    "the process of exploring the neighborhood around it.\n",
    "\n",
    "Scout bees are responsible for exploring the search space beyond\n",
    "the neighborhood of the food sources. If a food source has not\n",
    "been improved for a certain number of iterations, a scout bee\n",
    "is sent to explore a new region of the search space.\n",
    "\n",
    "The ABC algorithm continues to run until a stopping criterion is met,\n",
    "such as a maximum number of iterations or a minimum improvement threshold."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m35.0/35.0 MB\u001B[0m \u001B[31m4.6 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m00:01\u001B[0m00:01\u001B[0mm\r\n",
      "\u001B[?25hRequirement already satisfied: pandas>=1.2.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (1.5.3)\r\n",
      "Collecting cloudpickle>=1.5.0\r\n",
      "  Downloading cloudpickle-2.2.1-py3-none-any.whl (25 kB)\r\n",
      "Collecting scikit-learn>=0.23.1\r\n",
      "  Downloading scikit_learn-1.2.2-cp311-cp311-macosx_10_9_x86_64.whl (9.0 MB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m9.0/9.0 MB\u001B[0m \u001B[31m19.1 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m00:01\u001B[0m00:01\u001B[0m\r\n",
      "\u001B[?25hCollecting click>=7.1.2\r\n",
      "  Downloading click-8.1.3-py3-none-any.whl (96 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m96.6/96.6 kB\u001B[0m \u001B[31m3.7 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n",
      "\u001B[?25hCollecting redis>=2.10.6\r\n",
      "  Downloading redis-4.5.3-py3-none-any.whl (238 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m238.6/238.6 kB\u001B[0m \u001B[31m7.3 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n",
      "\u001B[?25hCollecting distributed>=2022.10.2\r\n",
      "  Downloading distributed-2023.3.2-py3-none-any.whl (956 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m956.9/956.9 kB\u001B[0m \u001B[31m15.2 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0ma \u001B[36m0:00:01\u001B[0m\r\n",
      "\u001B[?25hCollecting matplotlib>=3.3.0\r\n",
      "  Downloading matplotlib-3.7.1-cp311-cp311-macosx_10_12_x86_64.whl (7.4 MB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m7.4/7.4 MB\u001B[0m \u001B[31m15.6 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m00:01\u001B[0m00:01\u001B[0m\r\n",
      "\u001B[?25hCollecting sqlalchemy<2.0.0,>=1.3.18\r\n",
      "  Downloading SQLAlchemy-1.4.47-cp311-cp311-macosx_10_9_universal2.whl (1.6 MB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m1.6/1.6 MB\u001B[0m \u001B[31m16.7 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m00:01\u001B[0m00:01\u001B[0m\r\n",
      "\u001B[?25hCollecting jabbar>=0.0.10\r\n",
      "  Downloading jabbar-0.0.15-py3-none-any.whl (6.4 kB)\r\n",
      "Collecting gitpython>=3.1.7\r\n",
      "  Downloading GitPython-3.1.31-py3-none-any.whl (184 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m184.3/184.3 kB\u001B[0m \u001B[31m5.5 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m00:01\u001B[0m\r\n",
      "\u001B[?25hCollecting dask==2023.3.2\r\n",
      "  Downloading dask-2023.3.2-py3-none-any.whl (1.2 MB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m1.2/1.2 MB\u001B[0m \u001B[31m17.3 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0ma \u001B[36m0:00:01\u001B[0m\r\n",
      "\u001B[?25hRequirement already satisfied: jinja2>=2.10.3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (3.1.2)\r\n",
      "Collecting locket>=1.0.0\r\n",
      "  Downloading locket-1.0.0-py2.py3-none-any.whl (4.4 kB)\r\n",
      "Collecting msgpack>=1.0.0\r\n",
      "  Downloading msgpack-1.0.5-cp311-cp311-macosx_10_9_x86_64.whl (73 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m73.7/73.7 kB\u001B[0m \u001B[31m2.6 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n",
      "\u001B[?25hRequirement already satisfied: packaging>=20.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (23.0)\r\n",
      "Requirement already satisfied: psutil>=5.7.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (5.9.4)\r\n",
      "Requirement already satisfied: pyyaml>=5.3.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (6.0)\r\n",
      "Collecting sortedcontainers>=2.0.5\r\n",
      "  Downloading sortedcontainers-2.4.0-py2.py3-none-any.whl (29 kB)\r\n",
      "Collecting tblib>=1.6.0\r\n",
      "  Downloading tblib-1.7.0-py2.py3-none-any.whl (12 kB)\r\n",
      "Collecting toolz>=0.10.0\r\n",
      "  Downloading toolz-0.12.0-py3-none-any.whl (55 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m55.8/55.8 kB\u001B[0m \u001B[31m1.9 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n",
      "\u001B[?25hRequirement already satisfied: tornado>=6.0.3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (6.2)\r\n",
      "Collecting urllib3>=1.24.3\r\n",
      "  Using cached urllib3-1.26.15-py2.py3-none-any.whl (140 kB)\r\n",
      "Collecting zict>=2.1.0\r\n",
      "  Downloading zict-2.2.0-py2.py3-none-any.whl (23 kB)\r\n",
      "Collecting fsspec>=0.6.0\r\n",
      "  Downloading fsspec-2023.3.0-py3-none-any.whl (145 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m145.4/145.4 kB\u001B[0m \u001B[31m5.5 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n",
      "\u001B[?25hCollecting partd>=1.2.0\r\n",
      "  Downloading partd-1.3.0-py3-none-any.whl (18 kB)\r\n",
      "Collecting importlib-metadata>=4.13.0\r\n",
      "  Downloading importlib_metadata-6.1.0-py3-none-any.whl (21 kB)\r\n",
      "Collecting gitdb<5,>=4.0.1\r\n",
      "  Downloading gitdb-4.0.10-py3-none-any.whl (62 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m62.7/62.7 kB\u001B[0m \u001B[31m2.3 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n",
      "\u001B[?25hCollecting contourpy>=1.0.1\r\n",
      "  Downloading contourpy-1.0.7-cp311-cp311-macosx_10_9_x86_64.whl (244 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m244.2/244.2 kB\u001B[0m \u001B[31m7.6 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n",
      "\u001B[?25hCollecting cycler>=0.10\r\n",
      "  Downloading cycler-0.11.0-py3-none-any.whl (6.4 kB)\r\n",
      "Collecting fonttools>=4.22.0\r\n",
      "  Downloading fonttools-4.39.2-py3-none-any.whl (1.0 MB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m1.0/1.0 MB\u001B[0m \u001B[31m14.5 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0ma \u001B[36m0:00:01\u001B[0m\r\n",
      "\u001B[?25hCollecting kiwisolver>=1.0.1\r\n",
      "  Downloading kiwisolver-1.4.4-cp311-cp311-macosx_10_9_x86_64.whl (65 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m65.5/65.5 kB\u001B[0m \u001B[31m2.3 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n",
      "\u001B[?25hCollecting pillow>=6.2.0\r\n",
      "  Downloading Pillow-9.4.0-2-cp311-cp311-macosx_10_10_x86_64.whl (3.3 MB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m3.3/3.3 MB\u001B[0m \u001B[31m11.5 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m00:01\u001B[0m00:01\u001B[0m\r\n",
      "\u001B[?25hCollecting pyparsing>=2.3.1\r\n",
      "  Downloading pyparsing-3.0.9-py3-none-any.whl (98 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m98.3/98.3 kB\u001B[0m \u001B[31m3.6 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n",
      "\u001B[?25hRequirement already satisfied: python-dateutil>=2.7 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from matplotlib>=3.3.0->pyabc) (2.8.2)\r\n",
      "Requirement already satisfied: pytz>=2020.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pandas>=1.2.0->pyabc) (2023.2)\r\n",
      "Collecting joblib>=1.1.1\r\n",
      "  Downloading joblib-1.2.0-py3-none-any.whl (297 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m298.0/298.0 kB\u001B[0m \u001B[31m8.2 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n",
      "\u001B[?25hCollecting threadpoolctl>=2.0.0\r\n",
      "  Downloading threadpoolctl-3.1.0-py3-none-any.whl (14 kB)\r\n",
      "Collecting greenlet!=0.4.17\r\n",
      "  Downloading greenlet-2.0.2-cp311-cp311-macosx_10_9_universal2.whl (243 kB)\r\n",
      "\u001B[2K     \u001B[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001B[0m \u001B[32m243.0/243.0 kB\u001B[0m \u001B[31m8.0 MB/s\u001B[0m eta \u001B[36m0:00:00\u001B[0m\r\n",
      "\u001B[?25hCollecting smmap<6,>=3.0.1\r\n",
      "  Downloading smmap-5.0.0-py3-none-any.whl (24 kB)\r\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from jinja2>=2.10.3->distributed>=2022.10.2->pyabc) (2.1.2)\r\n",
      "Requirement already satisfied: six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from python-dateutil>=2.7->matplotlib>=3.3.0->pyabc) (1.16.0)\r\n",
      "Collecting heapdict\r\n",
      "  Downloading HeapDict-1.0.1-py3-none-any.whl (3.9 kB)\r\n",
      "Collecting zipp>=0.5\r\n",
      "  Downloading zipp-3.15.0-py3-none-any.whl (6.8 kB)\r\n",
      "Installing collected packages: sortedcontainers, msgpack, heapdict, zipp, zict, urllib3, toolz, threadpoolctl, tblib, smmap, scipy, redis, pyparsing, pillow, locket, kiwisolver, joblib, jabbar, greenlet, fsspec, fonttools, cycler, contourpy, cloudpickle, click, sqlalchemy, scikit-learn, partd, matplotlib, importlib-metadata, gitdb, gitpython, dask, distributed, pyabc\r\n",
      "Successfully installed click-8.1.3 cloudpickle-2.2.1 contourpy-1.0.7 cycler-0.11.0 dask-2023.3.2 distributed-2023.3.2 fonttools-4.39.2 fsspec-2023.3.0 gitdb-4.0.10 gitpython-3.1.31 greenlet-2.0.2 heapdict-1.0.1 importlib-metadata-6.1.0 jabbar-0.0.15 joblib-1.2.0 kiwisolver-1.4.4 locket-1.0.0 matplotlib-3.7.1 msgpack-1.0.5 partd-1.3.0 pillow-9.4.0 pyabc-0.12.9 pyparsing-3.0.9 redis-4.5.3 scikit-learn-1.2.2 scipy-1.10.1 smmap-5.0.0 sortedcontainers-2.4.0 sqlalchemy-1.4.47 tblib-1.7.0 threadpoolctl-3.1.0 toolz-0.12.0 urllib3-1.26.15 zict-2.2.0 zipp-3.15.0\r\n",
      "\r\n",
      "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m A new release of pip available: \u001B[0m\u001B[31;49m22.3.1\u001B[0m\u001B[39;49m -> \u001B[0m\u001B[32;49m23.0.1\u001B[0m\r\n",
      "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m To update, run: \u001B[0m\u001B[32;49mpip3 install --upgrade pip\u001B[0m\r\n",
      "Requirement already satisfied: pandas in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (1.5.3)\r\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pandas) (2.8.2)\r\n",
      "Requirement already satisfied: pytz>=2020.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pandas) (2023.2)\r\n",
      "Requirement already satisfied: numpy>=1.21.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pandas) (1.24.2)\r\n",
      "Requirement already satisfied: six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)\r\n",
      "\r\n",
      "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m A new release of pip available: \u001B[0m\u001B[31;49m22.3.1\u001B[0m\u001B[39;49m -> \u001B[0m\u001B[32;49m23.0.1\u001B[0m\r\n",
      "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m To update, run: \u001B[0m\u001B[32;49mpip3 install --upgrade pip\u001B[0m\r\n",
      "zsh:1: command not found: pip\r\n",
      "Requirement already satisfied: pyabc in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (0.12.9)\r\n",
      "Requirement already satisfied: numpy>=1.19.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (1.24.2)\r\n",
      "Requirement already satisfied: scipy>=1.5.2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (1.10.1)\r\n",
      "Requirement already satisfied: pandas>=1.2.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (1.5.3)\r\n",
      "Requirement already satisfied: cloudpickle>=1.5.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (2.2.1)\r\n",
      "Requirement already satisfied: scikit-learn>=0.23.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (1.2.2)\r\n",
      "Requirement already satisfied: click>=7.1.2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (8.1.3)\r\n",
      "Requirement already satisfied: redis>=2.10.6 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (4.5.3)\r\n",
      "Requirement already satisfied: distributed>=2022.10.2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (2023.3.2)\r\n",
      "Requirement already satisfied: matplotlib>=3.3.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (3.7.1)\r\n",
      "Requirement already satisfied: sqlalchemy<2.0.0,>=1.3.18 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (1.4.47)\r\n",
      "Requirement already satisfied: jabbar>=0.0.10 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (0.0.15)\r\n",
      "Requirement already satisfied: gitpython>=3.1.7 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pyabc) (3.1.31)\r\n",
      "Requirement already satisfied: dask==2023.3.2 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (2023.3.2)\r\n",
      "Requirement already satisfied: jinja2>=2.10.3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (3.1.2)\r\n",
      "Requirement already satisfied: locket>=1.0.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (1.0.0)\r\n",
      "Requirement already satisfied: msgpack>=1.0.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (1.0.5)\r\n",
      "Requirement already satisfied: packaging>=20.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (23.0)\r\n",
      "Requirement already satisfied: psutil>=5.7.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (5.9.4)\r\n",
      "Requirement already satisfied: pyyaml>=5.3.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (6.0)\r\n",
      "Requirement already satisfied: sortedcontainers>=2.0.5 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (2.4.0)\r\n",
      "Requirement already satisfied: tblib>=1.6.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (1.7.0)\r\n",
      "Requirement already satisfied: toolz>=0.10.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (0.12.0)\r\n",
      "Requirement already satisfied: tornado>=6.0.3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (6.2)\r\n",
      "Requirement already satisfied: urllib3>=1.24.3 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (1.26.15)\r\n",
      "Requirement already satisfied: zict>=2.1.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from distributed>=2022.10.2->pyabc) (2.2.0)\r\n",
      "Requirement already satisfied: fsspec>=0.6.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from dask==2023.3.2->distributed>=2022.10.2->pyabc) (2023.3.0)\r\n",
      "Requirement already satisfied: partd>=1.2.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from dask==2023.3.2->distributed>=2022.10.2->pyabc) (1.3.0)\r\n",
      "Requirement already satisfied: importlib-metadata>=4.13.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from dask==2023.3.2->distributed>=2022.10.2->pyabc) (6.1.0)\r\n",
      "Requirement already satisfied: gitdb<5,>=4.0.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from gitpython>=3.1.7->pyabc) (4.0.10)\r\n",
      "Requirement already satisfied: contourpy>=1.0.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from matplotlib>=3.3.0->pyabc) (1.0.7)\r\n",
      "Requirement already satisfied: cycler>=0.10 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from matplotlib>=3.3.0->pyabc) (0.11.0)\r\n",
      "Requirement already satisfied: fonttools>=4.22.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from matplotlib>=3.3.0->pyabc) (4.39.2)\r\n",
      "Requirement already satisfied: kiwisolver>=1.0.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from matplotlib>=3.3.0->pyabc) (1.4.4)\r\n",
      "Requirement already satisfied: pillow>=6.2.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from matplotlib>=3.3.0->pyabc) (9.4.0)\r\n",
      "Requirement already satisfied: pyparsing>=2.3.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from matplotlib>=3.3.0->pyabc) (3.0.9)\r\n",
      "Requirement already satisfied: python-dateutil>=2.7 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from matplotlib>=3.3.0->pyabc) (2.8.2)\r\n",
      "Requirement already satisfied: pytz>=2020.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pandas>=1.2.0->pyabc) (2023.2)\r\n",
      "Requirement already satisfied: joblib>=1.1.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from scikit-learn>=0.23.1->pyabc) (1.2.0)\r\n",
      "Requirement already satisfied: threadpoolctl>=2.0.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from scikit-learn>=0.23.1->pyabc) (3.1.0)\r\n",
      "Requirement already satisfied: greenlet!=0.4.17 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from sqlalchemy<2.0.0,>=1.3.18->pyabc) (2.0.2)\r\n",
      "Requirement already satisfied: smmap<6,>=3.0.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from gitdb<5,>=4.0.1->gitpython>=3.1.7->pyabc) (5.0.0)\r\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from jinja2>=2.10.3->distributed>=2022.10.2->pyabc) (2.1.2)\r\n",
      "Requirement already satisfied: six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from python-dateutil>=2.7->matplotlib>=3.3.0->pyabc) (1.16.0)\r\n",
      "Requirement already satisfied: heapdict in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from zict>=2.1.0->distributed>=2022.10.2->pyabc) (1.0.1)\r\n",
      "Requirement already satisfied: zipp>=0.5 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from importlib-metadata>=4.13.0->dask==2023.3.2->distributed>=2022.10.2->pyabc) (3.15.0)\r\n",
      "\r\n",
      "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m A new release of pip available: \u001B[0m\u001B[31;49m22.3.1\u001B[0m\u001B[39;49m -> \u001B[0m\u001B[32;49m23.0.1\u001B[0m\r\n",
      "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m To update, run: \u001B[0m\u001B[32;49mpip3 install --upgrade pip\u001B[0m\r\n",
      "Requirement already satisfied: pandas in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (1.5.3)\r\n",
      "Requirement already satisfied: python-dateutil>=2.8.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pandas) (2.8.2)\r\n",
      "Requirement already satisfied: pytz>=2020.1 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pandas) (2023.2)\r\n",
      "Requirement already satisfied: numpy>=1.21.0 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from pandas) (1.24.2)\r\n",
      "Requirement already satisfied: six>=1.5 in /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)\r\n",
      "\r\n",
      "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m A new release of pip available: \u001B[0m\u001B[31;49m22.3.1\u001B[0m\u001B[39;49m -> \u001B[0m\u001B[32;49m23.0.1\u001B[0m\r\n",
      "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m To update, run: \u001B[0m\u001B[32;49mpip3 install --upgrade pip\u001B[0m\r\n",
      "Collecting sklearn\r\n",
      "  Downloading sklearn-0.0.post1.tar.gz (3.6 kB)\r\n",
      "  Preparing metadata (setup.py) ... \u001B[?25ldone\r\n",
      "\u001B[?25hInstalling collected packages: sklearn\r\n",
      "\u001B[33m  DEPRECATION: sklearn is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559\u001B[0m\u001B[33m\r\n",
      "\u001B[0m  Running setup.py install for sklearn ... \u001B[?25ldone\r\n",
      "\u001B[?25hSuccessfully installed sklearn-0.0.post1\r\n",
      "\r\n",
      "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m A new release of pip available: \u001B[0m\u001B[31;49m22.3.1\u001B[0m\u001B[39;49m -> \u001B[0m\u001B[32;49m23.0.1\u001B[0m\r\n",
      "\u001B[1m[\u001B[0m\u001B[34;49mnotice\u001B[0m\u001B[1;39;49m]\u001B[0m\u001B[39;49m To update, run: \u001B[0m\u001B[32;49mpip3 install --upgrade pip\u001B[0m\r\n"
     ]
    }
   ],
   "source": [
    "!pip3 install pyabc\n",
    "!pip3 install pandas\n",
    "!pip3 install sklearn"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Load the NSL-KDD dataset into pandas dataframe"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['duration', 'protocol_type', 'service', 'flag', 'src_bytes',\n",
      "       'dst_bytes', 'land', 'wrong_fragment', 'urgent', 'hot',\n",
      "       'num_failed_logins', 'logged_in', 'num_compromised', 'root_shell',\n",
      "       'su_attempted', 'num_root', 'num_file_creations', 'num_shells',\n",
      "       'num_access_files', 'num_outbound_cmds', 'is_host_login',\n",
      "       'is_guest_login', 'count', 'srv_count', 'serror_rate',\n",
      "       'srv_serror_rate', 'rerror_rate', 'srv_rerror_rate', 'same_srv_rate',\n",
      "       'diff_srv_rate', 'srv_diff_host_rate', 'dst_host_count',\n",
      "       'dst_host_srv_count', 'dst_host_same_srv_rate',\n",
      "       'dst_host_diff_srv_rate', 'dst_host_same_src_port_rate',\n",
      "       'dst_host_srv_diff_host_rate', 'dst_host_serror_rate',\n",
      "       'dst_host_srv_serror_rate', 'dst_host_rerror_rate',\n",
      "       'dst_host_srv_rerror_rate', 'label'],\n",
      "      dtype='object')\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# Load the NSL-KDD dataset into a Pandas dataframe\n",
    "\n",
    "col_names = [\"duration\",\"protocol_type\",\"service\",\"flag\",\"src_bytes\",\n",
    "    \"dst_bytes\",\"land\",\"wrong_fragment\",\"urgent\",\"hot\",\"num_failed_logins\",\n",
    "    \"logged_in\",\"num_compromised\",\"root_shell\",\"su_attempted\",\"num_root\",\n",
    "    \"num_file_creations\",\"num_shells\",\"num_access_files\",\"num_outbound_cmds\",\n",
    "    \"is_host_login\",\"is_guest_login\",\"count\",\"srv_count\",\"serror_rate\",\n",
    "    \"srv_serror_rate\",\"rerror_rate\",\"srv_rerror_rate\",\"same_srv_rate\",\n",
    "    \"diff_srv_rate\",\"srv_diff_host_rate\",\"dst_host_count\",\"dst_host_srv_count\",\n",
    "    \"dst_host_same_srv_rate\",\"dst_host_diff_srv_rate\",\"dst_host_same_src_port_rate\",\n",
    "    \"dst_host_srv_diff_host_rate\",\"dst_host_serror_rate\",\"dst_host_srv_serror_rate\",\n",
    "    \"dst_host_rerror_rate\",\"dst_host_srv_rerror_rate\",\"label\"]\n",
    "\n",
    "\n",
    "df = pd.read_csv('../NSL-KDD/NSL_KDD_Train.csv', header=None, names=col_names)\n",
    "\n",
    "\n",
    "print(df.columns)\n",
    "\n",
    "df.columns = df.columns.astype(str)\n",
    "\n",
    "# Access the dataframe using the correct column name\n",
    "X = df[['root_shell', 'urgent', 'protocol_type', 'num_file_creations', 'srv_count',\n",
    "        'service', 'dst_host_srv_serror_rate', 'dst_bytes', 'logged_in', 'hot',\n",
    "        'diff_srv_rate', 'land', 'wrong_fragment', 'flag',\n",
    "        'dst_host_serror_rate', 'is_host_login', 'num_shells', 'num_access_files',\n",
    "        'dst_host_count', 'srv_serror_rate', 'su_attempted',\n",
    "        'dst_host_srv_rerror_rate', 'count', 'num_failed_logins',\n",
    "        'dst_host_same_srv_rate', 'num_root', 'same_srv_rate', 'is_guest_login',\n",
    "        'dst_host_srv_count', 'num_outbound_cmds', 'srv_diff_host_rate',\n",
    "        'num_compromised', 'rerror_rate', 'dst_host_srv_diff_host_rate',\n",
    "        'srv_rerror_rate', 'serror_rate', 'src_bytes', 'dst_host_diff_srv_rate',\n",
    "        'duration', 'dst_host_rerror_rate', 'dst_host_same_src_port_rate']]\n",
    "y = df['label']\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Prepare the data by separating the features and labels, and normalizing the feature values"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": ".iloc requires numeric indexers, got ['root_shell' 'urgent' 'protocol_type' 'num_file_creations' 'srv_count'\n 'service' 'dst_host_srv_serror_rate' 'dst_bytes' 'logged_in' 'hot'\n 'diff_srv_rate' 'label' 'land' 'wrong_fragment' 'flag'\n 'dst_host_serror_rate' 'is_host_login' 'num_shells' 'num_access_files'\n 'dst_host_count' 'srv_serror_rate' 'su_attempted'\n 'dst_host_srv_rerror_rate' 'count' 'num_failed_logins'\n 'dst_host_same_srv_rate' 'num_root' 'same_srv_rate' 'is_guest_login'\n 'dst_host_srv_count' 'num_outbound_cmds' 'srv_diff_host_rate'\n 'num_compromised' 'rerror_rate' 'dst_host_srv_diff_host_rate'\n 'srv_rerror_rate' 'serror_rate' 'src_bytes' 'dst_host_diff_srv_rate'\n 'duration' 'dst_host_rerror_rate' 'dst_host_same_src_port_rate']",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mIndexError\u001B[0m                                Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[30], line 11\u001B[0m\n\u001B[1;32m      8\u001B[0m cat_data \u001B[38;5;241m=\u001B[39m pd\u001B[38;5;241m.\u001B[39mget_dummies(df\u001B[38;5;241m.\u001B[39miloc[:, cat_cols])\n\u001B[1;32m     10\u001B[0m \u001B[38;5;66;03m# Combine the one-hot encoded categorical features and numerical features\u001B[39;00m\n\u001B[0;32m---> 11\u001B[0m X \u001B[38;5;241m=\u001B[39m pd\u001B[38;5;241m.\u001B[39mconcat([cat_data, \u001B[43mdf\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43miloc\u001B[49m\u001B[43m[\u001B[49m\u001B[43m:\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mnum_cols\u001B[49m\u001B[43m]\u001B[49m], axis\u001B[38;5;241m=\u001B[39m\u001B[38;5;241m1\u001B[39m)\n\u001B[1;32m     13\u001B[0m \u001B[38;5;66;03m# Normalize the feature values\u001B[39;00m\n\u001B[1;32m     14\u001B[0m scaler \u001B[38;5;241m=\u001B[39m StandardScaler()\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/indexing.py:1067\u001B[0m, in \u001B[0;36m_LocationIndexer.__getitem__\u001B[0;34m(self, key)\u001B[0m\n\u001B[1;32m   1065\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_is_scalar_access(key):\n\u001B[1;32m   1066\u001B[0m         \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mobj\u001B[38;5;241m.\u001B[39m_get_value(\u001B[38;5;241m*\u001B[39mkey, takeable\u001B[38;5;241m=\u001B[39m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_takeable)\n\u001B[0;32m-> 1067\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_getitem_tuple\u001B[49m\u001B[43m(\u001B[49m\u001B[43mkey\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m   1068\u001B[0m \u001B[38;5;28;01melse\u001B[39;00m:\n\u001B[1;32m   1069\u001B[0m     \u001B[38;5;66;03m# we by definition only have the 0th axis\u001B[39;00m\n\u001B[1;32m   1070\u001B[0m     axis \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39maxis \u001B[38;5;129;01mor\u001B[39;00m \u001B[38;5;241m0\u001B[39m\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/indexing.py:1563\u001B[0m, in \u001B[0;36m_iLocIndexer._getitem_tuple\u001B[0;34m(self, tup)\u001B[0m\n\u001B[1;32m   1561\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m_getitem_tuple\u001B[39m(\u001B[38;5;28mself\u001B[39m, tup: \u001B[38;5;28mtuple\u001B[39m):\n\u001B[0;32m-> 1563\u001B[0m     tup \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_validate_tuple_indexer\u001B[49m\u001B[43m(\u001B[49m\u001B[43mtup\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m   1564\u001B[0m     \u001B[38;5;28;01mwith\u001B[39;00m suppress(IndexingError):\n\u001B[1;32m   1565\u001B[0m         \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_getitem_lowerdim(tup)\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/indexing.py:873\u001B[0m, in \u001B[0;36m_LocationIndexer._validate_tuple_indexer\u001B[0;34m(self, key)\u001B[0m\n\u001B[1;32m    871\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m i, k \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28menumerate\u001B[39m(key):\n\u001B[1;32m    872\u001B[0m     \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[0;32m--> 873\u001B[0m         \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_validate_key\u001B[49m\u001B[43m(\u001B[49m\u001B[43mk\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mi\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m    874\u001B[0m     \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m err:\n\u001B[1;32m    875\u001B[0m         \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\n\u001B[1;32m    876\u001B[0m             \u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mLocation based indexing can only have \u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m    877\u001B[0m             \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m[\u001B[39m\u001B[38;5;132;01m{\u001B[39;00m\u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_valid_types\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m] types\u001B[39m\u001B[38;5;124m\"\u001B[39m\n\u001B[1;32m    878\u001B[0m         ) \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01merr\u001B[39;00m\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pandas/core/indexing.py:1477\u001B[0m, in \u001B[0;36m_iLocIndexer._validate_key\u001B[0;34m(self, key, axis)\u001B[0m\n\u001B[1;32m   1475\u001B[0m \u001B[38;5;66;03m# check that the key has a numeric dtype\u001B[39;00m\n\u001B[1;32m   1476\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m is_numeric_dtype(arr\u001B[38;5;241m.\u001B[39mdtype):\n\u001B[0;32m-> 1477\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mIndexError\u001B[39;00m(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124m.iloc requires numeric indexers, got \u001B[39m\u001B[38;5;132;01m{\u001B[39;00marr\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m)\n\u001B[1;32m   1479\u001B[0m \u001B[38;5;66;03m# check that the key does not exceed the maximum size of the index\u001B[39;00m\n\u001B[1;32m   1480\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mlen\u001B[39m(arr) \u001B[38;5;129;01mand\u001B[39;00m (arr\u001B[38;5;241m.\u001B[39mmax() \u001B[38;5;241m>\u001B[39m\u001B[38;5;241m=\u001B[39m len_axis \u001B[38;5;129;01mor\u001B[39;00m arr\u001B[38;5;241m.\u001B[39mmin() \u001B[38;5;241m<\u001B[39m \u001B[38;5;241m-\u001B[39mlen_axis):\n",
      "\u001B[0;31mIndexError\u001B[0m: .iloc requires numeric indexers, got ['root_shell' 'urgent' 'protocol_type' 'num_file_creations' 'srv_count'\n 'service' 'dst_host_srv_serror_rate' 'dst_bytes' 'logged_in' 'hot'\n 'diff_srv_rate' 'label' 'land' 'wrong_fragment' 'flag'\n 'dst_host_serror_rate' 'is_host_login' 'num_shells' 'num_access_files'\n 'dst_host_count' 'srv_serror_rate' 'su_attempted'\n 'dst_host_srv_rerror_rate' 'count' 'num_failed_logins'\n 'dst_host_same_srv_rate' 'num_root' 'same_srv_rate' 'is_guest_login'\n 'dst_host_srv_count' 'num_outbound_cmds' 'srv_diff_host_rate'\n 'num_compromised' 'rerror_rate' 'dst_host_srv_diff_host_rate'\n 'srv_rerror_rate' 'serror_rate' 'src_bytes' 'dst_host_diff_srv_rate'\n 'duration' 'dst_host_rerror_rate' 'dst_host_same_src_port_rate']"
     ]
    }
   ],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "\n",
    "# Separate the categorical and numerical features\n",
    "cat_cols = [1, 2, 3]\n",
    "num_cols = list(set(df.columns) - set(cat_cols))\n",
    "\n",
    "# One-hot encode the categorical features\n",
    "cat_data = pd.get_dummies(df.iloc[:, cat_cols])\n",
    "\n",
    "# Combine the one-hot encoded categorical features and numerical features\n",
    "X = pd.concat([cat_data, df.iloc[:, num_cols]], axis=1)\n",
    "\n",
    "# Normalize the feature values\n",
    "scaler = StandardScaler()\n",
    "X = scaler.fit_transform(X)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Define the objective function: In ABC, the objective function is used to evaluate the quality of candidate solutions. In the context of intrusion detection, the objective function could be the accuracy of a machine learning model trained on the NSL-KDD dataset."
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "# Split the data into training and testing sets\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "# Define the objective function\n",
    "def objective_function(params):\n",
    "    n_estimators, max_depth = params\n",
    "    # Train a Random Forest Classifier with the specified parameters\n",
    "    clf = RandomForestClassifier(n_estimators=n_estimators, max_depth=max_depth, random_state=42)\n",
    "    clf.fit(X_train, y_train)\n",
    "    # Evaluate the model on the testing set and return the accuracy\n",
    "    y_pred = clf.predict(X_test)\n",
    "    return accuracy_score(y_test, y_pred)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Define the search space: In ABC, the search space is the range of values that the algorithm can search to find the optimal solution. For example, in the context of intrusion detection, the search space could be the range of values for the number of trees in a Random Forest Classifier and the maximum depth of the trees"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "# Define the search space\n",
    "search_space = [\n",
    "    (\"n_estimators\", (50, 200)),\n",
    "    (\"max_depth\", (2, 20))\n",
    "]"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Once the objective function and search space are defined, you can run the ABC algorithm to find the optimal solution"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'str' object has no attribute 'items'",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mAttributeError\u001B[0m                            Traceback (most recent call last)",
      "Cell \u001B[0;32mIn[34], line 6\u001B[0m\n\u001B[1;32m      4\u001B[0m distributions \u001B[38;5;241m=\u001B[39m {}\n\u001B[1;32m      5\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m name, (min_val, max_val) \u001B[38;5;129;01min\u001B[39;00m search_space:\n\u001B[0;32m----> 6\u001B[0m     distributions[name] \u001B[38;5;241m=\u001B[39m \u001B[43mDistribution\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43muniform\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmin_val\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[43mmax_val\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m      8\u001B[0m \u001B[38;5;66;03m# Run the ABC algorithm\u001B[39;00m\n\u001B[1;32m      9\u001B[0m abc \u001B[38;5;241m=\u001B[39m ABCSMC(objective_function, distributions)\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyabc/parameters.py:30\u001B[0m, in \u001B[0;36mParameterStructure.__init__\u001B[0;34m(self, *args, **kwargs)\u001B[0m\n\u001B[1;32m     28\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mException\u001B[39;00m(\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mOnly keyword or dictionary allowed\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[1;32m     29\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mlen\u001B[39m(args) \u001B[38;5;241m>\u001B[39m \u001B[38;5;241m0\u001B[39m:\n\u001B[0;32m---> 30\u001B[0m     flattened \u001B[38;5;241m=\u001B[39m \u001B[43mParameterStructure\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mflatten_dict\u001B[49m\u001B[43m(\u001B[49m\u001B[43margs\u001B[49m\u001B[43m[\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m]\u001B[49m\u001B[43m)\u001B[49m\n\u001B[1;32m     31\u001B[0m \u001B[38;5;28;01melif\u001B[39;00m \u001B[38;5;28mlen\u001B[39m(kwargs) \u001B[38;5;241m>\u001B[39m \u001B[38;5;241m0\u001B[39m:\n\u001B[1;32m     32\u001B[0m     flattened \u001B[38;5;241m=\u001B[39m ParameterStructure\u001B[38;5;241m.\u001B[39mflatten_dict(kwargs)\n",
      "File \u001B[0;32m/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyabc/parameters.py:17\u001B[0m, in \u001B[0;36mParameterStructure.flatten_dict\u001B[0;34m(dict_)\u001B[0m\n\u001B[1;32m     14\u001B[0m \u001B[38;5;129m@staticmethod\u001B[39m\n\u001B[1;32m     15\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21mflatten_dict\u001B[39m(dict_: \u001B[38;5;28mdict\u001B[39m):\n\u001B[1;32m     16\u001B[0m     new_dict \u001B[38;5;241m=\u001B[39m {}\n\u001B[0;32m---> 17\u001B[0m     \u001B[38;5;28;01mfor\u001B[39;00m key, value \u001B[38;5;129;01min\u001B[39;00m \u001B[43mdict_\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mitems\u001B[49m():\n\u001B[1;32m     18\u001B[0m         \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(value, \u001B[38;5;28mdict\u001B[39m):\n\u001B[1;32m     19\u001B[0m             flattened \u001B[38;5;241m=\u001B[39m ParameterStructure\u001B[38;5;241m.\u001B[39mflatten_dict(value)\n",
      "\u001B[0;31mAttributeError\u001B[0m: 'str' object has no attribute 'items'"
     ]
    }
   ],
   "source": [
    "from pyabc import ABCSMC, Distribution\n",
    "\n",
    "# Define the distributions for the search space\n",
    "distributions = {}\n",
    "for name, (min_val, max_val) in search_space:\n",
    "    distributions[name] = Distribution(\"uniform\", min_val, max_val)\n",
    "\n",
    "# Run the ABC algorithm\n",
    "abc = ABCSMC(objective_function, distributions)\n",
    "history = abc.run()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
